﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CountJobTags{
    /// <summary>
    /// An object that corresponds to a single tag for a job on Stack Overflow, other information that applies to
    /// the entire job, such as city/state/allowsRemote
    /// </summary>
    /// <remarks>
    /// This class is pretty straightforward, it basically just holds the property values and exposes them with a name. The constructor
    /// does a bit of parsing and figures-out how to display the
    /// </remarks>
    public class JobDesc : ICloneable {
        #region Properties
        /// <summary>Assigned by the web site</summary>
        public string ID { get; set; }
        /// <summary>Such as 'javascript'</summary>
        public string Tag { get; set; }
        /// <summary>How long the ad has been posted</summary>
        public TimeSpan Age { get; set; }

        private string _AgeString;
        /// <summary>The text the web site used to describe the age of the job offer</summary>
        public string AgeString {
            get { return _AgeString; }
            set {
                int p = value.IndexOf("&lt;");
                if (p == 0) {
                    _AgeString = "<" + value.Substring(4);
                } else if (p > 0)
                    _AgeString = value.Substring(0, p) + "<" + value.Substring(p + 4);
                else
                    _AgeString = value;
            }
        }

        /// <summary>Where the job is located</summary>
        public string City { get; set; }
        /// <summary>Province where the state is located</summary>
        public string Province { get; set; }
        /// <summary>Country, if any, where the job is located</summary>
        public string Country { get; set; }
        /// <summary>Whether the position will allow remote work</summary>
        public bool AllowsRemote { get; set; }
        /// <summary>Web page number where the job is located</summary>
        public int PageNumber { get; set; }
        /// <summary>Total number of pages of jobs listed</summary>
        public int PageTotal { get; set; }

        #endregion

        #region Constructors
        public JobDesc() { }

        public JobDesc(string inTag, string inAgeString, string inId, string city, string state, string country, bool allowsRemote, int pageNumber = 0) {
            this.Tag = inTag;
            this.AgeString = inAgeString.Replace("\r", "").Replace("&lt;", "<");
            this.ID = inId;
            this.City = city;
            this.Province = string.IsNullOrWhiteSpace(state) ? "NA" : state;
            this.Country = country;
            this.AllowsRemote = allowsRemote;

            //Try parsing the age string, which assumes a variety of formats, such as "3 days ago", "< 1 hour ago", etc.
            int p = inAgeString.IndexOf(' ');
            int number = 0;
            if (p > 0) {
                int.TryParse(inAgeString.Substring(0, p), out number);
            }

            if (inAgeString.Contains("weeks ago") || inAgeString.Contains("week ago")) {
                this.Age = DateTime.Now - DateTime.Now.AddDays(-7 * number);
            } else if (inAgeString == "< 1 hour ago") {
                this.Age = new TimeSpan(0);
            } else if (inAgeString.Contains("hour ago") || inAgeString.Contains("hours ago")) {
                this.Age = DateTime.Now - DateTime.Now.AddHours(-number);
            } else if (inAgeString.Contains("days ago")) {
                this.Age = DateTime.Now - DateTime.Now.AddDays(-number);
            } else if (inAgeString.StartsWith("yesterday")) {
                this.Age = DateTime.Now - DateTime.Now.AddDays(-1);
            } else {
                //hopefully this never happens, but if it does, I want to know so I can deal with it
                //throw new ArgumentException("Unrecognized age format: '" + inAgeString + "'");
                Console.WriteLine("Unrecognized age format: '" + inAgeString + "'");
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Builds a tab-separated string with all the properties
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            return string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", ID, Tag, AgeString, City, Province, Country, AllowsRemote);
        }
        #endregion

        public object Clone() {
            return this.MemberwiseClone();
        }
    }
}
